﻿using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public static class CameraCurveFactory
    {
        private static Dictionary<string, CameraCurveConfig> mCache = new Dictionary<string, CameraCurveConfig>();

        public static CameraCurveConfig GetConfig(string path)
        {
            if (mCache.TryGetValue(path, out var rConfig) && rConfig != null)
            {
                return rConfig;
            }

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                rConfig = AssetDatabase.LoadAssetAtPath<CameraCurveConfig>("Assets/" + path);
            }
            else
#endif
            {
                var rLoader = ResLoader.Create();
                rConfig = rLoader.LoadABAsset<CameraCurveConfig>(path)?.Clone();
                rLoader.Put2Pool();
            }
            if (rConfig != null)
            {
                mCache[path] = rConfig;
            }
            return rConfig;
        }

        public static void Clear()
        {
            mCache.Clear();
        }
    }
}